package com.wisdom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.wisdom.adapter.MyAdapter;
import com.wisdom.http.HttpNewsService;
import com.wisdom.http.HttpRequestUrl;
import com.wisdom.utils.ActivityUtil;
import com.wisdom.view.MyListView;
import com.wisdom.view.MyListView.OnRefreshListener;
import com.wisdom.vo.News;


public class NewsMoreContentActivity extends Activity {

	private ImageButton btn_back;
	private TextView tv_title;
	private MyListView listView;
	private String path;//url路径
	private String text;//顶部动态文字

	private List<News> list;

	private MyAdapter adapter;

	private ViewSwitcher viewSwitcher;

	private Map<String,String> params;
	private HttpNewsService newsService;
	private Button button;
	private ActivityUtil activityUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_news_more_content);

		Bundle bundle = getIntent().getExtras();
		if (bundle!=null) {
			path = bundle.getString("more_news_list_url");
			text = bundle.getString("text");
		}
		initViews();
		requestRSSFeed();

	}

	private void initViews() {
		btn_back = (ImageButton) findViewById(R.id.btn_news_more_content_back);
		btn_back.setOnClickListener(onclick_listener);
		
		tv_title = (TextView) findViewById(R.id.tv_news_more_content_title);
		tv_title.setText(text);
		button = new Button(this);
		button.setText("查看更多的新闻");
		button.setHint(21+"");
		activityUtil = new ActivityUtil();
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int startIndex = Integer.parseInt(button.getHint().toString());
				int endIndex = startIndex+ActivityUtil.COUNT;
				list = activityUtil.nextPage(list, startIndex,endIndex,path);
				if(list==null){
					ActivityUtil.toast(NewsMoreContentActivity.this, "没有更多的新闻加载了");
				}else{
					adapter.notifyDataSetChanged();
					button.setHint(endIndex+"");
				}
			}
		});
		
		viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher_news_more_content);
		listView = new MyListView(this);
		listView.setCacheColorHint(Color.argb(0, 0, 0, 0));
		listView.addFooterView(button);
		listView.setonRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				new AsyncTask<Map<String,String>, Integer, List<News>>(){

					@Override
					protected List<News> doInBackground(Map<String, String>... params) {
						try {
							Thread.sleep(1000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						data();
						return list;
					}
					@Override
					protected void onPostExecute(List<News> result) {
						button.setHint(21+"");
						listView.setAdapter(new MyAdapter(list, NewsMoreContentActivity.this));
						listView.onRefreshComplete();
					}
					
				}.execute();
			}
		});
		
		viewSwitcher.addView(listView);
		viewSwitcher.addView(getLayoutInflater().inflate(R.layout.layout_progress_page, null));
		viewSwitcher.showNext();
		listView.setOnItemClickListener(listener);

		newsService = new HttpNewsService();
		params = new HashMap<String, String>();

	}
  
	private void requestRSSFeed() {
		Thread t = new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					data();
					if (list.size() == 0) {
						handler.sendEmptyMessage(-1);
					} else {
						handler.sendEmptyMessage(1);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		t.start();
	}
	private void data(){
		params.clear();
		params.put("news.newsTitle", path);
		String data = newsService.requestByPost(HttpRequestUrl.url(HttpRequestUrl.NEWS_SELECT),params);
		list = newsService.parseNews(data);
		
	}
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				adapter = new MyAdapter(list,NewsMoreContentActivity.this);
				listView.setOnItemClickListener(listener);
				listView.setAdapter(adapter);
				viewSwitcher.showPrevious();
			}
		};
	};

	private OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Intent intent = new Intent(NewsMoreContentActivity.this, NewsContentActivity.class);
			intent.putExtra("content_url", list.get(position-1).getNewsId()+"");
			intent.putExtra("count", list.get(position-1).getCommentsCount()+"");
			NewsMoreContentActivity.this.startActivityForResult(intent, position);
		}
	};
	
	private OnClickListener onclick_listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			finish();
		}
	};
}
